<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Python爬虫课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>JSON模块与JsonPath | Python爬虫课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-tbfed-pagefooter/footer.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../file/part02/2.8.html" />
    
    
    <link rel="prev" href="../../file/part02/2.6.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.7"
        data-chapter-title="JSON模块与JsonPath"
        data-filepath="file/part02/2.7.md"
        data-basepath="../.."
        data-revision="Thu Feb 09 2017 09:48:59 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        传智播客Python学院爬虫课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="file/part01/1.html">
            
                
                    <a href="../../file/part01/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        爬虫原理与数据抓取
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="file/part01/1.1.html">
            
                
                    <a href="../../file/part01/1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        (了解)通用爬虫和聚焦爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="file/part01/1.2.html">
            
                
                    <a href="../../file/part01/1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        (复习)HTTP/HTTPS的请求与响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="file/part01/1.3.html">
            
                
                    <a href="../../file/part01/1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        HTTP/HTTPS抓包工具-Fiddler
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="file/part01/1.4.html">
            
                
                    <a href="../../file/part01/1.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        urllib2模块的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="file/part01/1.5.html">
            
                
                    <a href="../../file/part01/1.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        urllib2：GET请求和POST请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="file/part01/1.6.html">
            
                
                    <a href="../../file/part01/1.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        urllib2：Handler处理器和自定义Opener
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="file/part01/1.7.html">
            
                
                    <a href="../../file/part01/1.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        urllib2：URLError与HTTPError
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="file/part01/1.8.html">
            
                
                    <a href="../../file/part01/1.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        Requests模块
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="file/part02/2.html">
            
                
                    <a href="../../file/part02/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        非结构化数据与结构化数据提取
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="file/part02/2.1.html">
            
                
                    <a href="../../file/part02/2.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        正则表达式re模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="file/part02/2.2.html">
            
                
                    <a href="../../file/part02/2.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        案例：使用正则表达式的爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="file/part02/2.3.html">
            
                
                    <a href="../../file/part02/2.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        XPath与lxml类库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="file/part02/2.4.html">
            
                
                    <a href="../../file/part02/2.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        案例：使用XPath的爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="file/part02/2.5.html">
            
                
                    <a href="../../file/part02/2.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        BeautifulSoup4 解析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="file/part02/2.6.html">
            
                
                    <a href="../../file/part02/2.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        案例：使用bs4的爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.7" data-path="file/part02/2.7.html">
            
                
                    <a href="../../file/part02/2.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        JSON模块与JsonPath
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="file/part02/2.8.html">
            
                
                    <a href="../../file/part02/2.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        糗事百科案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="file/part02/2.9.html">
            
                
                    <a href="../../file/part02/2.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        多线程爬虫案例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="file/part03/3.html">
            
                
                    <a href="../../file/part03/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        动态HTML处理和机器图像识别
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="file/part03/3.1.html">
            
                
                    <a href="../../file/part03/3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        动态HTML介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="file/part03/3.2.html">
            
                
                    <a href="../../file/part03/3.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Selenium与PhantomJS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="file/part03/3.3.html">
            
                
                    <a href="../../file/part03/3.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        案例一：网站模拟登录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="file/part03/3.4.html">
            
                
                    <a href="../../file/part03/3.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        案例二：动态页面模拟点击
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="file/part03/3.5.html">
            
                
                    <a href="../../file/part03/3.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        案例三：执行JavaScript语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="file/part03/3.6.html">
            
                
                    <a href="../../file/part03/3.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        机器视觉与Tesseract介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="file/part03/3.7.html">
            
                
                    <a href="../../file/part03/3.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        处理一些格式规范的文字
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="file/part03/3.8.html">
            
                
                    <a href="../../file/part03/3.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        案例：尝试对验证码进行机器识别处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="file/part03/3.9.html">
            
                
                    <a href="../../file/part03/3.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        机器学习：训练Tesseract
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="file/part04/4.html">
            
                
                    <a href="../../file/part04/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        Scrapy框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="file/part04/4.1.html">
            
                
                    <a href="../../file/part04/4.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        配置安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="file/part04/4.2.html">
            
                
                    <a href="../../file/part04/4.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        入门案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="file/part04/4.3.html">
            
                
                    <a href="../../file/part04/4.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Scrapy Shell
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="file/part04/4.4.html">
            
                
                    <a href="../../file/part04/4.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Item Pipeline
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="file/part04/4.5.html">
            
                
                    <a href="../../file/part04/4.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        Spiders
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="file/part04/4.6.html">
            
                
                    <a href="../../file/part04/4.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        CrawlSpiders
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="file/part04/4.7.html">
            
                
                    <a href="../../file/part04/4.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        Request/Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="file/part04/4.8.html">
            
                
                    <a href="../../file/part04/4.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        Downloader Middlewares
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="file/part04/4.9.html">
            
                
                    <a href="../../file/part04/4.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        Settings
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="file/part05/5.html">
            
                
                    <a href="../../file/part05/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        Scrapy实战项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="file/part05/5.1.html">
            
                
                    <a href="../../file/part05/5.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        (案例一)手机App抓包爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="file/part05/5.2.html">
            
                
                    <a href="../../file/part05/5.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        (案例二)阳光热线问政平台爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="file/part05/5.3.html">
            
                
                    <a href="../../file/part05/5.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        (案例三)新浪网分类资讯爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="file/part05/5.4.html">
            
                
                    <a href="../../file/part05/5.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        (案例四)图片下载器爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="file/part05/5.5.html">
            
                
                    <a href="../../file/part05/5.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        (案例五)将数据保存在MongoDB中
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="file/part05/5.6.html">
            
                
                    <a href="../../file/part05/5.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        (案例六)三种scrapy模拟登陆策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="file/part05/5.7.html">
            
                
                    <a href="../../file/part05/5.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        附：通过Fiddler进行手机抓包方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="file/part06/6.html">
            
                
                    <a href="../../file/part06/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        scrapy-redis分布式组件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="file/part06/6.1.html">
            
                
                    <a href="../../file/part06/6.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        源码分析参考：Connection
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="file/part06/6.2.html">
            
                
                    <a href="../../file/part06/6.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        源码分析参考：Dupefilter
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="file/part06/6.3.html">
            
                
                    <a href="../../file/part06/6.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        源码分析参考：Picklecompat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="file/part06/6.4.html">
            
                
                    <a href="../../file/part06/6.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        源码分析参考：Pipelines
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="file/part06/6.5.html">
            
                
                    <a href="../../file/part06/6.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        源码分析参考：Queue
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="file/part06/6.6.html">
            
                
                    <a href="../../file/part06/6.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        源码分析参考：Scheduler
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="file/part06/6.7.html">
            
                
                    <a href="../../file/part06/6.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        源码分析参考：Spider
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="file/part07/7.html">
            
                
                    <a href="../../file/part07/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        scrapy-redis实战
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="file/part07/7.1.html">
            
                
                    <a href="../../file/part07/7.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        源码自带项目说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="file/part07/7.2.html">
            
                
                    <a href="../../file/part07/7.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        有缘网分布式爬虫项目1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="file/part07/7.3.html">
            
                
                    <a href="../../file/part07/7.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        有缘网分布式爬虫项目2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="file/part07/7.4.html">
            
                
                    <a href="../../file/part07/7.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        处理Redis里的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="file/part07/7.5.html">
            
                
                    <a href="../../file/part07/7.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        尝试改写新浪网分类资讯爬虫1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="file/part07/7.6.html">
            
                
                    <a href="../../file/part07/7.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        尝试改写新浪网分类资讯爬虫2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.7" data-path="file/part07/7.7.html">
            
                
                    <a href="../../file/part07/7.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.7.</b>
                        
                        IT桔子分布式项目1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.8" data-path="file/part07/7.8.html">
            
                
                    <a href="../../file/part07/7.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.8.</b>
                        
                        IT桔子分布式项目2
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="file/duanzi/duanzi.html">
            
                
                    <a href="../../file/duanzi/duanzi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        课余段子
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Python爬虫课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x6570;&#x636E;&#x63D0;&#x53D6;&#x4E4B;json&#x4E0E;jsonpath">&#x6570;&#x636E;&#x63D0;&#x53D6;&#x4E4B;JSON&#x4E0E;JsonPATH</h1>
<p>JSON(JavaScript Object Notation) &#x662F;&#x4E00;&#x79CD;&#x8F7B;&#x91CF;&#x7EA7;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x6362;&#x683C;&#x5F0F;&#xFF0C;&#x5B83;&#x4F7F;&#x5F97;&#x4EBA;&#x4EEC;&#x5F88;&#x5BB9;&#x6613;&#x7684;&#x8FDB;&#x884C;&#x9605;&#x8BFB;&#x548C;&#x7F16;&#x5199;&#x3002;&#x540C;&#x65F6;&#x4E5F;&#x65B9;&#x4FBF;&#x4E86;&#x673A;&#x5668;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#x548C;&#x751F;&#x6210;&#x3002;&#x9002;&#x7528;&#x4E8E;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x6BD4;&#x5982;&#x7F51;&#x7AD9;&#x524D;&#x53F0;&#x4E0E;&#x540E;&#x53F0;&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x3002;</p>
<p>JSON&#x548C;XML&#x7684;&#x6BD4;&#x8F83;&#x53EF;&#x8C13;&#x4E0D;&#x76F8;&#x4E0A;&#x4E0B;&#x3002;</p>
<p>Python 2.7&#x4E2D;&#x81EA;&#x5E26;&#x4E86;JSON&#x6A21;&#x5757;&#xFF0C;&#x76F4;&#x63A5;<code>import json</code>&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E86;&#x3002;</p>
<p>&#x5B98;&#x65B9;&#x6587;&#x6863;&#xFF1A;<a href="http://docs.python.org/library/json.html" target="_blank">http://docs.python.org/library/json.html</a></p>
<p>Json&#x5728;&#x7EBF;&#x89E3;&#x6790;&#x7F51;&#x7AD9;&#xFF1A;<a href="http://www.json.cn/#" target="_blank">http://www.json.cn/#</a></p>
<h1 id="json">JSON</h1>
<p>json&#x7B80;&#x5355;&#x8BF4;&#x5C31;&#x662F;javascript&#x4E2D;&#x7684;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x4E24;&#x79CD;&#x7ED3;&#x6784;&#x5C31;&#x662F;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x4E24;&#x79CD;&#x7ED3;&#x6784;&#xFF0C;&#x901A;&#x8FC7;&#x8FD9;&#x4E24;&#x79CD;&#x7ED3;&#x6784;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x5404;&#x79CD;&#x590D;&#x6742;&#x7684;&#x7ED3;&#x6784;</p>
<blockquote>
<ol>
<li><p>&#x5BF9;&#x8C61;&#xFF1A;&#x5BF9;&#x8C61;&#x5728;js&#x4E2D;&#x8868;&#x793A;&#x4E3A;<code>{ }</code>&#x62EC;&#x8D77;&#x6765;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E3A; <code>{ key&#xFF1A;value, key&#xFF1A;value, ... }</code>&#x7684;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x5728;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7684;&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;key&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#xFF0C;value&#x4E3A;&#x5BF9;&#x5E94;&#x7684;&#x5C5E;&#x6027;&#x503C;&#xFF0C;&#x6240;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#xFF0C;&#x53D6;&#x503C;&#x65B9;&#x6CD5;&#x4E3A; &#x5BF9;&#x8C61;.key &#x83B7;&#x53D6;&#x5C5E;&#x6027;&#x503C;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x662F;&#x6570;&#x5B57;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x7EC4;&#x3001;&#x5BF9;&#x8C61;&#x8FD9;&#x51E0;&#x79CD;&#x3002;</p>
</li>
<li><p>&#x6570;&#x7EC4;&#xFF1A;&#x6570;&#x7EC4;&#x5728;js&#x4E2D;&#x662F;&#x4E2D;&#x62EC;&#x53F7;<code>[ ]</code>&#x62EC;&#x8D77;&#x6765;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E3A; <code>[&quot;Python&quot;, &quot;javascript&quot;, &quot;C++&quot;, ...]</code>&#xFF0C;&#x53D6;&#x503C;&#x65B9;&#x5F0F;&#x548C;&#x6240;&#x6709;&#x8BED;&#x8A00;&#x4E2D;&#x4E00;&#x6837;&#xFF0C;&#x4F7F;&#x7528;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#xFF0C;&#x5B57;&#x6BB5;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x662F; &#x6570;&#x5B57;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x7EC4;&#x3001;&#x5BF9;&#x8C61;&#x51E0;&#x79CD;&#x3002;</p>
</li>
</ol>
</blockquote>
<h1 id="import-json">import json</h1>
<p>json&#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x4E86;&#x56DB;&#x4E2A;&#x529F;&#x80FD;&#xFF1A;<code>dumps</code>&#x3001;<code>dump</code>&#x3001;<code>loads</code>&#x3001;<code>load</code>&#xFF0C;&#x7528;&#x4E8E;&#x5B57;&#x7B26;&#x4E32; &#x548C; python&#x6570;&#x636E;&#x7C7B;&#x578B;&#x95F4;&#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x3002;</p>
<h2 id="1-jsonloads">1. json.loads()</h2>
<p>&#x628A;Json&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x7801;&#x8F6C;&#x6362;&#x6210;Python&#x5BF9;&#x8C61;
&#x4ECE;json&#x5230;python&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x5316;&#x5BF9;&#x7167;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="../images/json2.png" alt=""></p>
<pre><code class="lang-python"><span class="hljs-comment"># json_loads.py</span>

<span class="hljs-keyword">import</span> json

strList = <span class="hljs-string">&apos;[1, 2, 3, 4]&apos;</span>

strDict = <span class="hljs-string">&apos;{&quot;city&quot;: &quot;&#x5317;&#x4EAC;&quot;, &quot;name&quot;: &quot;&#x5927;&#x732B;&quot;}&apos;</span>

json.loads(strList) 
<span class="hljs-comment"># [1, 2, 3, 4]</span>

json.loads(strDict) <span class="hljs-comment"># json&#x6570;&#x636E;&#x81EA;&#x52A8;&#x6309;Unicode&#x5B58;&#x50A8;</span>
<span class="hljs-comment"># {u&apos;city&apos;: u&apos;\u5317\u4eac&apos;, u&apos;name&apos;: u&apos;\u5927\u732b&apos;}</span>
</code></pre>
<h2 id="2-jsondumps">2. json.dumps()</h2>
<p>&#x5B9E;&#x73B0;python&#x7C7B;&#x578B;&#x8F6C;&#x5316;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;str&#x5BF9;&#x8C61;
&#x628A;&#x4E00;&#x4E2A;Python&#x5BF9;&#x8C61;&#x7F16;&#x7801;&#x8F6C;&#x6362;&#x6210;Json&#x5B57;&#x7B26;&#x4E32;</p>
<p>&#x4ECE;python&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x5411;json&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x5316;&#x5BF9;&#x7167;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="../images/json.png" alt=""></p>
<pre><code class="lang-python"><span class="hljs-comment"># json_dumps.py</span>

<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> chardet

listStr = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
tupleStr = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
dictStr = {<span class="hljs-string">&quot;city&quot;</span>: <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;&#x5927;&#x732B;&quot;</span>}

json.dumps(listStr)
<span class="hljs-comment"># &apos;[1, 2, 3, 4]&apos;</span>
json.dumps(tupleStr)
<span class="hljs-comment"># &apos;[1, 2, 3, 4]&apos;</span>

<span class="hljs-comment"># &#x6CE8;&#x610F;&#xFF1A;json.dumps() &#x5E8F;&#x5217;&#x5316;&#x65F6;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;&#x7684;ascii&#x7F16;&#x7801;</span>
<span class="hljs-comment"># &#x6DFB;&#x52A0;&#x53C2;&#x6570; ensure_ascii=False &#x7981;&#x7528;ascii&#x7F16;&#x7801;&#xFF0C;&#x6309;utf-8&#x7F16;&#x7801;</span>
<span class="hljs-comment"># chardet.detect()&#x8FD4;&#x56DE;&#x5B57;&#x5178;, &#x5176;&#x4E2D;confidence&#x662F;&#x68C0;&#x6D4B;&#x7CBE;&#x786E;&#x5EA6;</span>

json.dumps(dictStr) 
<span class="hljs-comment"># &apos;{&quot;city&quot;: &quot;\\u5317\\u4eac&quot;, &quot;name&quot;: &quot;\\u5927\\u5218&quot;}&apos;</span>

chardet.detect(json.dumps(dictStr))
<span class="hljs-comment"># {&apos;confidence&apos;: 1.0, &apos;encoding&apos;: &apos;ascii&apos;}</span>

<span class="hljs-keyword">print</span> json.dumps(dictStr, ensure_ascii=<span class="hljs-keyword">False</span>) 
<span class="hljs-comment"># {&quot;city&quot;: &quot;&#x5317;&#x4EAC;&quot;, &quot;name&quot;: &quot;&#x5927;&#x5218;&quot;}</span>

chardet.detect(json.dumps(dictStr, ensure_ascii=<span class="hljs-keyword">False</span>))
<span class="hljs-comment"># {&apos;confidence&apos;: 0.99, &apos;encoding&apos;: &apos;utf-8&apos;}</span>
</code></pre>
<p><strong><em>chardet&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x4F18;&#x79C0;&#x7684;&#x7F16;&#x7801;&#x8BC6;&#x522B;&#x6A21;&#x5757;&#xFF0C;&#x53EF;&#x901A;&#x8FC7;pip&#x5B89;&#x88C5;</em></strong></p>
<h2 id="3-jsondump">3. json.dump()</h2>
<p>&#x5C06;Python&#x5185;&#x7F6E;&#x7C7B;&#x578B;&#x5E8F;&#x5217;&#x5316;&#x4E3A;json&#x5BF9;&#x8C61;&#x540E;&#x5199;&#x5165;&#x6587;&#x4EF6;</p>
<pre><code class="lang-python"><span class="hljs-comment"># json_dump.py</span>

<span class="hljs-keyword">import</span> json

listStr = [{<span class="hljs-string">&quot;city&quot;</span>: <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>}, {<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;&#x5927;&#x5218;&quot;</span>}]
json.dump(listStr, open(<span class="hljs-string">&quot;listStr.json&quot;</span>,<span class="hljs-string">&quot;w&quot;</span>), ensure_ascii=<span class="hljs-keyword">False</span>)

dictStr = {<span class="hljs-string">&quot;city&quot;</span>: <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;&#x5927;&#x5218;&quot;</span>}
json.dump(dictStr, open(<span class="hljs-string">&quot;dictStr.json&quot;</span>,<span class="hljs-string">&quot;w&quot;</span>), ensure_ascii=<span class="hljs-keyword">False</span>)
</code></pre>
<h2 id="4-jsonload">4. json.load()</h2>
<p>&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x4E2D;json&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5143;&#x7D20; &#x8F6C;&#x5316;&#x6210;python&#x7C7B;&#x578B;</p>
<pre><code class="lang-python"><span class="hljs-comment"># json_load.py</span>

<span class="hljs-keyword">import</span> json

strList = json.load(open(<span class="hljs-string">&quot;listStr.json&quot;</span>))
<span class="hljs-keyword">print</span> strList

<span class="hljs-comment"># [{u&apos;city&apos;: u&apos;\u5317\u4eac&apos;}, {u&apos;name&apos;: u&apos;\u5927\u5218&apos;}]</span>

strDict = json.load(open(<span class="hljs-string">&quot;dictStr.json&quot;</span>))
<span class="hljs-keyword">print</span> strDict
<span class="hljs-comment"># {u&apos;city&apos;: u&apos;\u5317\u4eac&apos;, u&apos;name&apos;: u&apos;\u5927\u5218&apos;}</span>
</code></pre>
<h1 id="jsonpath">JsonPath</h1>
<p>JsonPath &#x662F;&#x4E00;&#x79CD;&#x4FE1;&#x606F;&#x62BD;&#x53D6;&#x7C7B;&#x5E93;&#xFF0C;&#x662F;&#x4ECE;JSON&#x6587;&#x6863;&#x4E2D;&#x62BD;&#x53D6;&#x6307;&#x5B9A;&#x4FE1;&#x606F;&#x7684;&#x5DE5;&#x5177;&#xFF0C;&#x63D0;&#x4F9B;&#x591A;&#x79CD;&#x8BED;&#x8A00;&#x5B9E;&#x73B0;&#x7248;&#x672C;&#xFF0C;&#x5305;&#x62EC;&#xFF1A;Javascript, Python&#xFF0C; PHP &#x548C; Java&#x3002;</p>
<p>JsonPath &#x5BF9;&#x4E8E; JSON &#x6765;&#x8BF4;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E; XPATH &#x5BF9;&#x4E8E; XML&#x3002;</p>
<blockquote>
<p>&#x4E0B;&#x8F7D;&#x5730;&#x5740;&#xFF1A;<a href="https://pypi.python.org/pypi/jsonpath/" target="_blank">https://pypi.python.org/pypi/jsonpath</a></p>
<p>&#x5B89;&#x88C5;&#x65B9;&#x6CD5;&#xFF1A;&#x70B9;&#x51FB;<code>Download URL</code>&#x94FE;&#x63A5;&#x4E0B;&#x8F7D;jsonpath&#xFF0C;&#x89E3;&#x538B;&#x4E4B;&#x540E;&#x6267;&#x884C;<code>python setup.py install</code></p>
<p>&#x5B98;&#x65B9;&#x6587;&#x6863;&#xFF1A;<a href="http://goessner.net/articles/JsonPath/" target="_blank">http://goessner.net/articles/JsonPath</a></p>
</blockquote>
<h2 id="jsonpath&#x4E0E;xpath&#x8BED;&#x6CD5;&#x5BF9;&#x6BD4;&#xFF1A;">JsonPath&#x4E0E;XPath&#x8BED;&#x6CD5;&#x5BF9;&#x6BD4;&#xFF1A;</h2>
<p>Json&#x7ED3;&#x6784;&#x6E05;&#x6670;&#xFF0C;&#x53EF;&#x8BFB;&#x6027;&#x9AD8;&#xFF0C;&#x590D;&#x6742;&#x5EA6;&#x4F4E;&#xFF0C;&#x975E;&#x5E38;&#x5BB9;&#x6613;&#x5339;&#x914D;&#xFF0C;&#x4E0B;&#x8868;&#x4E2D;&#x5BF9;&#x5E94;&#x4E86;XPath&#x7684;&#x7528;&#x6CD5;&#x3002;</p>
<table>
<thead>
<tr>
<th style="text-align:center">XPath</th>
<th>JSONPath</th>
<th>&#x63CF;&#x8FF0;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>/</code></td>
<td><code>$</code></td>
<td>&#x6839;&#x8282;&#x70B9;</td>
</tr>
<tr>
<td style="text-align:center"><code>.</code></td>
<td><code>@</code></td>
<td>&#x73B0;&#x884C;&#x8282;&#x70B9;</td>
</tr>
<tr>
<td style="text-align:center"><code>/</code></td>
<td><code>.</code>or<code>[]</code></td>
<td>&#x53D6;&#x5B50;&#x8282;&#x70B9;</td>
</tr>
<tr>
<td style="text-align:center"><code>..</code></td>
<td>n/a</td>
<td>&#x53D6;&#x7236;&#x8282;&#x70B9;&#xFF0C;Jsonpath&#x672A;&#x652F;&#x6301;</td>
</tr>
<tr>
<td style="text-align:center"><code>//</code></td>
<td><code>..</code></td>
<td>&#x5C31;&#x662F;&#x4E0D;&#x7BA1;&#x4F4D;&#x7F6E;&#xFF0C;&#x9009;&#x62E9;&#x6240;&#x6709;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6761;&#x4EF6;</td>
</tr>
<tr>
<td style="text-align:center"><code>*</code></td>
<td><code>*</code></td>
<td>&#x5339;&#x914D;&#x6240;&#x6709;&#x5143;&#x7D20;&#x8282;&#x70B9;</td>
</tr>
<tr>
<td style="text-align:center"><code>@</code></td>
<td>n/a</td>
<td>&#x6839;&#x636E;&#x5C5E;&#x6027;&#x8BBF;&#x95EE;&#xFF0C;Json&#x4E0D;&#x652F;&#x6301;&#xFF0C;&#x56E0;&#x4E3A;Json&#x662F;&#x4E2A;Key-value&#x9012;&#x5F52;&#x7ED3;&#x6784;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x3002;</td>
</tr>
<tr>
<td style="text-align:center"><code>[]</code></td>
<td><code>[]</code></td>
<td>&#x8FED;&#x4EE3;&#x5668;&#x6807;&#x793A;&#xFF08;&#x53EF;&#x4EE5;&#x5728;&#x91CC;&#x8FB9;&#x505A;&#x7B80;&#x5355;&#x7684;&#x8FED;&#x4EE3;&#x64CD;&#x4F5C;&#xFF0C;&#x5982;&#x6570;&#x7EC4;&#x4E0B;&#x6807;&#xFF0C;&#x6839;&#x636E;&#x5185;&#x5BB9;&#x9009;&#x503C;&#x7B49;&#xFF09;</td>
</tr>
<tr>
<td style="text-align:center">|</td>
<td><code>[,]</code></td>
<td>&#x652F;&#x6301;&#x8FED;&#x4EE3;&#x5668;&#x4E2D;&#x505A;&#x591A;&#x9009;&#x3002;</td>
</tr>
<tr>
<td style="text-align:center"><code>[]</code></td>
<td><code>?()</code></td>
<td>&#x652F;&#x6301;&#x8FC7;&#x6EE4;&#x64CD;&#x4F5C;.</td>
</tr>
<tr>
<td style="text-align:center">n/a</td>
<td><code>()</code></td>
<td>&#x652F;&#x6301;&#x8868;&#x8FBE;&#x5F0F;&#x8BA1;&#x7B97;</td>
</tr>
<tr>
<td style="text-align:center"><code>()</code></td>
<td>n/a</td>
<td>&#x5206;&#x7EC4;&#xFF0C;JsonPath&#x4E0D;&#x652F;&#x6301;</td>
</tr>
</tbody>
</table>
<h2 id="&#x793A;&#x4F8B;&#xFF1A;">&#x793A;&#x4F8B;&#xFF1A;</h2>
<p>&#x6211;&#x4EEC;&#x4EE5;&#x62C9;&#x52FE;&#x7F51;&#x57CE;&#x5E02;JSON&#x6587;&#x4EF6; <a href="http://www.lagou.com/lbs/getAllCitySearchLabels.json" target="_blank">http://www.lagou.com/lbs/getAllCitySearchLabels.json</a> &#x4E3A;&#x4F8B;&#xFF0C;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x57CE;&#x5E02;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># jsonpath_lagou.py</span>

<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> jsonpath
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> chardet

url = <span class="hljs-string">&apos;http://www.lagou.com/lbs/getAllCitySearchLabels.json&apos;</span>
request =urllib2.Request(url)
response = urllib2.urlopen(request)
html = response.read()

<span class="hljs-comment"># &#x628A;json&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;python&#x5BF9;&#x8C61;</span>
jsonobj = json.loads(html)

<span class="hljs-comment"># &#x4ECE;&#x6839;&#x8282;&#x70B9;&#x5F00;&#x59CB;&#xFF0C;&#x5339;&#x914D;name&#x8282;&#x70B9;</span>
citylist = jsonpath.jsonpath(jsonobj,<span class="hljs-string">&apos;$..name&apos;</span>)

<span class="hljs-keyword">print</span> citylist
<span class="hljs-keyword">print</span> type(citylist)
fp = open(<span class="hljs-string">&apos;city.json&apos;</span>,<span class="hljs-string">&apos;w&apos;</span>)

content = json.dumps(citylist, ensure_ascii=<span class="hljs-keyword">False</span>)
<span class="hljs-keyword">print</span> content

fp.write(content.encode(<span class="hljs-string">&apos;utf-8&apos;</span>))
fp.close()
</code></pre>
<h2 id="&#x6CE8;&#x610F;&#x4E8B;&#x9879;&#xFF1A;">&#x6CE8;&#x610F;&#x4E8B;&#x9879;&#xFF1A;</h2>
<p>json.loads() &#x662F;&#x628A; Json&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x7801;&#x8F6C;&#x6362;&#x6210;Python&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x5728;json.loads&#x7684;&#x65F6;&#x5019;&#x51FA;&#x9519;&#xFF0C;&#x8981;&#x6CE8;&#x610F;&#x88AB;&#x89E3;&#x7801;&#x7684;Json&#x5B57;&#x7B26;&#x7684;&#x7F16;&#x7801;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F20;&#x5165;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7F16;&#x7801;&#x4E0D;&#x662F;UTF-8&#x7684;&#x8BDD;&#xFF0C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x5B57;&#x7B26;&#x7F16;&#x7801;&#x7684;&#x53C2;&#x6570;
<code>encoding</code></p>
<pre><code class="lang-python">dataDict = json.loads(jsonStrGBK);
</code></pre>
<ul>
<li><p>dataJsonStr&#x662F;JSON&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5047;&#x8BBE;&#x5176;&#x7F16;&#x7801;&#x672C;&#x8EAB;&#x662F;&#x975E;UTF-8&#x7684;&#x8BDD;&#x800C;&#x662F;GBK &#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4F1A;&#x5BFC;&#x81F4;&#x51FA;&#x9519;&#xFF0C;&#x6539;&#x4E3A;&#x5BF9;&#x5E94;&#x7684;&#xFF1A;</p>
<pre><code class="lang-python">  dataDict = json.loads(jsonStrGBK, encoding=<span class="hljs-string">&quot;GBK&quot;</span>);
</code></pre>
</li>
<li><p>&#x5982;&#x679C; dataJsonStr&#x901A;&#x8FC7;encoding&#x6307;&#x5B9A;&#x4E86;&#x5408;&#x9002;&#x7684;&#x7F16;&#x7801;&#xFF0C;&#x4F46;&#x662F;&#x5176;&#x4E2D;&#x53C8;&#x5305;&#x542B;&#x4E86;&#x5176;&#x4ED6;&#x7F16;&#x7801;&#x7684;&#x5B57;&#x7B26;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x5148;&#x53BB;&#x5C06;dataJsonStr&#x8F6C;&#x6362;&#x4E3A;Unicode&#xFF0C;&#x7136;&#x540E;&#x518D;&#x6307;&#x5B9A;&#x7F16;&#x7801;&#x683C;&#x5F0F;&#x8C03;&#x7528;json.loads() </p>
</li>
</ul>
<pre><code>``` python
</code></pre><p>dataJsonStrUni = dataJsonStr.decode(&quot;GB2312&quot;); 
dataDict = json.loads(dataJsonStrUni, encoding=&quot;GB2312&quot;);</p>
<pre><code>
##&#x5B57;&#x7B26;&#x4E32;&#x7F16;&#x7801;&#x8F6C;&#x6362;

&#x8FD9;&#x662F;&#x4E2D;&#x56FD;&#x7A0B;&#x5E8F;&#x5458;&#x6700;&#x82E6;&#x903C;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x4EC0;&#x4E48;&#x4E71;&#x7801;&#x4E4B;&#x7C7B;&#x7684;&#x51E0;&#x4E4E;&#x90FD;&#x662F;&#x7531;&#x6C49;&#x5B57;&#x5F15;&#x8D77;&#x7684;&#x3002;
&#x5176;&#x5B9E;&#x7F16;&#x7801;&#x95EE;&#x9898;&#x5F88;&#x597D;&#x641E;&#x5B9A;&#xFF0C;&#x53EA;&#x8981;&#x8BB0;&#x4F4F;&#x4E00;&#x70B9;&#xFF1A;

####&#x4EFB;&#x4F55;&#x5E73;&#x53F0;&#x7684;&#x4EFB;&#x4F55;&#x7F16;&#x7801; &#x90FD;&#x80FD;&#x548C; Unicode &#x4E92;&#x76F8;&#x8F6C;&#x6362;

UTF-8 &#x4E0E; GBK &#x4E92;&#x76F8;&#x8F6C;&#x6362;&#xFF0C;&#x90A3;&#x5C31;&#x5148;&#x628A;UTF-8&#x8F6C;&#x6362;&#x6210;Unicode&#xFF0C;&#x518D;&#x4ECE;Unicode&#x8F6C;&#x6362;&#x6210;GBK&#xFF0C;&#x53CD;&#x4E4B;&#x540C;&#x7406;&#x3002;



``` python 
# &#x8FD9;&#x662F;&#x4E00;&#x4E2A; UTF-8 &#x7F16;&#x7801;&#x7684;&#x5B57;&#x7B26;&#x4E32;
utf8Str = &quot;&#x4F60;&#x597D;&#x5730;&#x7403;&quot;

# 1. &#x5C06; UTF-8 &#x7F16;&#x7801;&#x7684;&#x5B57;&#x7B26;&#x4E32; &#x8F6C;&#x6362;&#x6210; Unicode &#x7F16;&#x7801;
unicodeStr = utf8Str.decode(&quot;UTF-8&quot;)

# 2. &#x518D;&#x5C06; Unicode &#x7F16;&#x7801;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32; &#x8F6C;&#x6362;&#x6210; GBK &#x7F16;&#x7801;
gbkData = unicodeStr.encode(&quot;GBK&quot;)

# 1. &#x518D;&#x5C06; GBK &#x7F16;&#x7801;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32; &#x8F6C;&#x5316;&#x6210; Unicode
unicodeStr = gbkData.decode(&quot;gbk&quot;)

# 2. &#x518D;&#x5C06; Unicode &#x7F16;&#x7801;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210; UTF-8
utf8Str = unicodeStr.encode(&quot;UTF-8&quot;)
</code></pre><p><code>decode</code>&#x7684;&#x4F5C;&#x7528;&#x662F;&#x5C06;&#x5176;&#x4ED6;&#x7F16;&#x7801;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210; Unicode &#x7F16;&#x7801;</p>
<p><code>encode</code>&#x7684;&#x4F5C;&#x7528;&#x662F;&#x5C06; Unicode &#x7F16;&#x7801;&#x8F6C;&#x6362;&#x6210;&#x5176;&#x4ED6;&#x7F16;&#x7801;&#x7684;&#x5B57;&#x7B26;&#x4E32;</p>
<p><code>&#x4E00;&#x53E5;&#x8BDD;&#xFF1A;UTF-8&#x662F;&#x5BF9;Unicode&#x5B57;&#x7B26;&#x96C6;&#x8FDB;&#x884C;&#x7F16;&#x7801;&#x7684;&#x4E00;&#x79CD;&#x7F16;&#x7801;&#x65B9;&#x5F0F;</code></p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; BigCat all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x300C;Revision Time:
2016-12-06 23:35:43&#x300D;
</span></footer>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../file/part02/2.6.html" class="navigation navigation-prev " aria-label="Previous page: 案例：使用bs4的爬虫"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../file/part02/2.8.html" class="navigation navigation-next " aria-label="Next page: 糗事百科案例"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © BigCat","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Python爬虫课程讲义 -->
</html>
